const app = getApp()
import {
  HomeIndex
} from 'index-modal.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new HomeIndex(); //实例化 首页 对象



Page({
  data: {
    is_tan:false,
    myobj:{},
    isauth:false,
    phone:''
  },
  onLoad: function(options) {
    let that=this;
    // that.setData({
    //   memberss: wx.getStorageSync('members')
    // })
   
  },

  getImage(){
    let myobj =  wx.getStorageSync('members') || [];
    if (Array.isArray(myobj)){
      this.setData({
        isauth:true
      })
    }else{
      this.setData({
        isauth: false
      })
    }
    this.setData({myobj})
  },

  _getPeople(){
     let that = this;
     let data = {
       id:wx.getStorageSync('members')['id']
     };
    index.getPeople(data,(res)=>{
      if(res.code === 200){
        that.setData({
          getPeople:res.data,
          couponNum: res.data.coupon,
          phone: res.data.phone
        })
      }
    })
  },

  getPhoneNumber:function(e){
    let that=this;
    let iv = e.detail.iv;
    let encryptedData = e.detail.encryptedData;
    wx.login({
      success: function (res){
        wx.request({
          url: Config.restUrl + 'weix/getOpenid',
          method: 'POST',
          data: {
            code: res.code
          },
          success: function (res) {
            let datas = {
              iv: iv,
              encryptedData: encryptedData,
              session_key: res.data.data.session_key
            }
            index.bindPhone(datas, (res) => {
              if (res.code == 200) {
                that.perfect(res.data.phoneNumber);
              } else {
                
              }
            })
          },
          fail: function (res) {
          }
        })
      }
    })
    // that.bindPhone(iv, encryptedData);
  },
  //綁定手機號
  perfect: function (phone){
    let that=this;
    let datas={
      uid: wx.getStorageSync('members').id,
      phone: phone,
      // code: code,
      type:2
    }
    index.perfect(datas,(res)=>{
      Function.layer(res.msg);
      that._getPeople();
    })
  },
  //手机号码解密
  // bindPhone: function (iv, encryptedData){
  //   let that=this;
  //   let datas={
  //     iv: iv,
  //     encryptedData: encryptedData,
  //     session_key: wx.getStorageSync('session_key')
  //   }
  //   index.bindPhone(datas,(res)=>{
  //     if (res.code==200){
  //       that.perfect(res.data.phoneNumber);
  //     }else {
        
  //     }
  //   })
  // },
  info(){
  	wx.navigateTo({url:'/pages/my/desc/desc'})
  },
  go_order(){
    wx.switchTab({
      url:"/pages/bill/noconsume/noconsume?status=1"
    })
  },
  getauth(){
    wx.navigateTo({
      url:'/pages/auth/auth'
    })
  },
  onShow:function(){
    this.getImage()
    this._getPeople()
  }

  
})